package net.trade.service.trade;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import net.core.base.dao.CommonDAO;
import net.core.common.SpringContext;
import net.core.service.cache.CacheManager;
import net.core.service.cache.CacheService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.trade.base.bean.MiyinMarket;
import net.trade.common.CacheConst;
import net.trade.common.Constants;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MarketJob{
	private static final Log logger = LogFactory.getLog(MarketJob.class);
	
	@Autowired
	private CacheManager cacheManager;
//	@Override
//	public void execute(JobExecutionContext context) throws JobExecutionException {
//		// TODO Auto-generated method stub
//		
//	}
	public void getMarket(){
		logger.debug("开始调度定时程序");
		System.out.println("start" +(new Date()));
		long time = System.currentTimeMillis();
		String result="";
		CommonDAO dao = (CommonDAO)SpringContext.getBean("commonDAO");
		MiyinMarket mm = new MiyinMarket();
		try {
			URL my_url = new URL("http://www.tjpme.com/mytest.php?rand=" + time);

			BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream(),"utf-8"));
			String strTemp = "";
			while (null != (strTemp = br.readLine())) {
				//System.out.println(strTemp.substring(1));
				// JSONObject obj=
				// JSONObject.fromObject("{"+strTemp.substring(1));
				// String s = obj.getString("sliver");
				// System.out.println(s);
				JSONArray arr = JSONArray.fromObject(strTemp);
				// arr.get(1);
				for (int i = 0; i < arr.size(); i++) {
					JSONObject obj = arr.getJSONObject(i);
					if (obj.has("sliver")) {
						result = obj.getString("sliver");
						String[] as = obj.getString("sliver").split("　");
//						for (int k = 0; k < as.length; k++) {
//							System.out.println(k + ": " + as[k]);
//							if (as[k].indexOf("最新价：") != -1) {
//								//System.out.println(as[k].substring("最新价：".length()));
//								result = as[k].substring("最新价：".length());
//							}
//						}
						//获取日期
						String date = as[0];
						mm.setDate(date);
						String stime = as[1].split("  ")[1];
						mm.setTime(stime);
						// 最新价
						String price = as[2].substring("最新价：".length());
						mm.setPoint(price);
					}
				}
				// System.out.println(as.length);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mm.setName("米银");
		
		CacheService cacheService = cacheManager.getCacheService();
		// 判断缓存中是否存在，如果存在从缓存中取值；如果不存在从数据库查询后放入缓存并返回
		cacheService.put(CacheConst.CACHE_MARKET, Constants.MARKET_PRODUCT, mm);
		cacheService.put(CacheConst.CACHE_MIYIN, Constants.MIYIN_PRODUCT, result);
		//System.out.println(result);
	}

}
